package com.android.launcher3.widget;

import static android.widget.LinearLayout.HORIZONTAL;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.launcher3.R;
import com.android.launcher3.Utilities;

import java.util.Calendar;

public class DateWidget extends FrameLayout {

    private TextView tvTime;
    private TextView tvDate;
    private Handler handler;

    private int[] weeks = new int[]{R.string.sun, R.string.mon, R.string.tue, R.string.wed, R.string.thu, R.string.fri, R.string.sat};
    private int[] months = new int[]{R.string.jan, R.string.feb, R.string.mar, R.string.apr, R.string.may, R.string.jun, R.string.jul, R.string.aug, R.string.sept, R.string.oct, R.string.nov, R.string.dec};

    private LinearLayout container;

    public DateWidget(@NonNull Context context) {
        super(context);
        init();
    }

    public DateWidget(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DateWidget(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init() {
        handler = new Handler();
        LayoutInflater.from(getContext()).inflate(R.layout.layout_date, this, true);
        container = findViewById(R.id.container);
        tvTime = findViewById(R.id.tv_time);
        tvDate = findViewById(R.id.tv_date);
        updateTime();
    }

    public void useAutoType() {
        post(() -> {

            DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

            container.setPadding(Utilities.pxFromDp(10, displayMetrics), 0, Utilities.pxFromDp(10, displayMetrics), 0);

        });
    }


    @SuppressLint("SetTextI18n")
    void updateTime() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        tvTime.setText(formateString(hour) + ":" + formateString(minute));
        tvDate.setText(getResources().getString(weeks[dayOfWeek - 1]) + "," + getResources().getString(months[month]) + " " + formateString(day));

        handler.postDelayed(this::updateTime, 1000 * 60);

    }

    @SuppressLint("DefaultLocale")
    String formateString(int i) {
        return String.format("%02d", i);
    }

}
